import CURDHelper from '@/utils/CURDHelper'
import PageConfig from '@/utils/pageConfig'
import { getBtnCtr } from '@/utils/BtnControl'
import Query from '@/components/Query';
import { VxeUI } from 'vxe-pc-ui'
import serverConfig from '../../serverConfig'
export default {
  name: 'FileCenter',
  components: {
    Query
  },
  data() {
    return {
      componentSize: PageConfig.ComponentSize,
      duration: PageConfig.Duration,
      btnPromission: '',
      BaseUrlApi: '/api/Xmwjwjj',
      ChildUrlApi: '/api/Xmwjwjmx',
      PagerConfig: {
        align: 'center',
        pageSize: 25,
        total: 0,
        currentPage: 1,
        pageSizes: [10, 25, 50, 100, 200],
      },
      treeList: [],
      checkNodeKey: 0,
      QueryConfig: [{
        title: '文件名称',
        field: 'wjmc',
        type: 'input',
        value: '',
        placeholder: '请输入文件名称以查询',
      }
      ],
      QueryConditions: [],
      GridOptions: {
        id: 'FileCenter_MainGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 700,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent: true
        },
        checkboxConfig: {
          highlight: true,
          range: true
        },
        exportConfig: {
        },
        toolbarConfig: {
          custom: true,
          slots: {
            buttons: 'customButtons'
          }
        },
        customConfig: {
          mode: 'drawer',
          storage: true
        },
        columns: [
          { type: 'seq', width: 50 },
          { type: 'checkbox', width: 35 },
          { field: 'wjmc', title: '文件名称', minWidth: 500, showOverflow: true },
          { field: 'wjlx', title: '类型', minWidth: 80, showOverflow: true },
          { field: 'wjdx', title: '文件大小(KB)', minWidth: 100, showOverflow: true },
          { field: 'createtime', title: '创建时间', minWidth: 150, showOverflow: true },
          { field: 'createname', title: '创建人', minWidth: 100, showOverflow: true },
          { field: 'updatetime', title: '更新时间', minWidth: 150, showOverflow: true },
          { field: 'updatename', title: '更新人', minWidth: 100, showOverflow: true },
          { field: 'operation', title: '操作', fixed: 'right', width: 60, slots: { default: 'operation' } },
        ],
        data: []
      },
      fileList:[],

    }
  },
  created() {
    // 动态头部按钮
    this.btnPromission = getBtnCtr(
      this.$route.path,
      this.$store.getters.userPermission
    );
    this.GetTreeList();
  },
  computed: {
    UploadUrl: function () {
        return serverConfig.production + 'api/Files/FilesUpload';
    },
  },
  methods: {
    handleFolderAdd(node) {
      this.$prompt('请输入文件夹名称', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        if (!value) {
          VxeUI.modal.message({ content: '请输入文件夹名称', status: 'error', duration: this.duration })
          return;
        }
        this.DoAdd(value, node);
      })
    },
    handleFolderEdit(node) {
      this.$prompt('请输入文件夹名称', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue: node.title
      }).then(({ value }) => {
        if (!value) {
          VxeUI.modal.message({ content: '请输入文件夹名称', status: 'error', duration: this.duration })
          return;
        }
        this.DoEdit(value, node);
      })
    },
    handleFolderDelete(node) {
      this.DoDelete(node);
    },
    handleDownload() {
      
    },
    handleTreeNodeClick({node}){
      let e = [{
        column:'wjjid',
        content:node.id,
        condition:'0'
      }]
      this.DoQuery(e);
    },
    handleQuery(e) {
      this.QueryConditions = e;
      this.DoQuery(e);
    },
    handlePageChange() {
      this.DoQuery(this.QueryConditions);
    },
    handleFileUploadSuccess(res) {
      this.DoAddChild(res);
    },
    handleBeforeUpload(){
      if(this.checkNodeKey == 0){
        VxeUI.modal.message({ content: `请选择文件夹后再进行上传操作`, status: 'error', duration: this.duration });
        return false;
      }
    },
    handleDelete(row){
      this.DoDeleteChild(row);
    },
    handleDownload(){
      var rows = this.$refs.maingrid.getCheckboxRecords(true);
      if(!rows || rows.length == 0){
        VxeUI.modal.message({ content: `请选择要下载的文件`, status: 'error', duration: this.duration });
        return;
      }
      this.DoDownload(rows);
    },
    GetTreeList() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, `${this.BaseUrlApi}/GetTree`).then(res => {
        this.treeList = [res.data];
      })
    },
    DoAdd(value, node) {
      let postData = { wjjmc: value, pid: node.id, id: 0, }
      CURDHelper.Add(postData, null, this.BaseUrlApi).then(res => {
        VxeUI.modal.message({ content: '操作成功', status: 'success', duration: this.duration })
        this.GetTreeList();
      }).catch(err => {
        VxeUI.modal.message({ content: `操作失败，${err}`, status: 'error', duration: this.duration })
      })
    },
    DoEdit(value, node) {
      let postData = { wjjmc: value, id: node.id }
      CURDHelper.Edit(postData, null, this.BaseUrlApi).then(res => {
        VxeUI.modal.message({ content: '操作成功', status: 'success', duration: this.duration })
        this.GetTreeList();
      }).catch(err => {
        VxeUI.modal.message({ content: `操作失败，${err}`, status: 'error', duration: this.duration })
      })
    },
    DoDelete(node) {
      if (node.id == 0) {
        VxeUI.modal.message({ content: '根目录不能删除', status: 'error', duration: this.duration })
        return;
      }
      let postData = { id: node.id }
      CURDHelper.DeleteWithConfirm('删除该文件夹会同时删除文件夹下的文件，是否继续删除？', postData, null, this.BaseUrlApi).then(res => {
        VxeUI.modal.message({ content: '操作成功', status: 'success', duration: this.duration })
        this.GetTreeList();
      }).catch(err => {
        VxeUI.modal.message({ content: `操作失败，${err}`, status: 'error', duration: this.duration })
      })
    },
    DoQuery(e){
      let queryList = e || [];
      if(this.checkNodeKey > 0){
        queryList.push({
          column:'wjjid',
          content:this.checkNodeKey,
          condition:'0'
        })
      }
      let request = {
        isPaging: true,
        pageSize:this.PagerConfig.pageSize,
        pageIndex:this.PagerConfig.currentPage,
        queryConditions:e,
        orderByConditions:[]
      }
      CURDHelper.Query(request,this.ChildUrlApi)
        .then(res=>{
          this.GridOptions.data = res.data;
          this.PagerConfig.total = res.totalNumber;
        }).catch(err=>{
            VxeUI.modal.message({
              content: err,
              status:'error',
              duration: this.duration
            })
        })
    },
    DoAddChild(fileInfo) {
      let postData = {
        id:0,
        wjjid: this.checkNodeKey,
        wjmc:fileInfo.Wjmc,
        wjlx:fileInfo.Wjlx,
        wjdx:fileInfo.Wjdx,
        wjlj:fileInfo.Wjlj
      }
      CURDHelper.Add(postData, null, this.ChildUrlApi).then(res => {
        this.DoQuery(this.QueryConditions);
      }).catch(err => {
        VxeUI.modal.message({ content: `操作失败，${err}`, status: 'error', duration: this.duration })
      })
    },
    DoDeleteChild(row) {
      let postData = { id: row.id }
      CURDHelper.DeleteWithConfirm('是否删除该文件？', postData, null, this.ChildUrlApi).then(res => {
        VxeUI.modal.message({ content: '操作成功', status: 'success', duration: this.duration })
        this.DoQuery(this.QueryConditions);
      }).catch(err => {
        VxeUI.modal.message({ content: `操作失败，${err}`, status: 'error', duration: this.duration })
      })
    },
    DoDownload(rows){
      for(let i = 0; i < rows.length; i++){
        fetch(`${serverConfig.production}${rows[i].wjlj}`)
        .then(response => {
          if(response.status != 200){
            VxeUI.modal.message({ content: `${rows[i].wjmc}${rows[i].wjlx}文件不存在,`, status: 'error', duration: this.duration })
            return null; 
          }
          VxeUI.saveFile({ filename: `${rows[i].wjmc}${rows[i].wjlx}`, content: response.blob() })
        })
        
      }
    }
  }
}
